/** @file
 * Declaration of a singleton base class
 */
#ifndef WII_GI_SINGLETON_H
#define WII_GI_SINGLETON_H

namespace wiigi
{
   /**
    * A simple templated class to provide a single interface
    */
   template<typename T >
   class c_Singleton
   {
      public:
         
         /**
          * returns a reference to the singleton class
          */
         static T* getInstance( void )
         {
            if( 0 == m_Instance )
            {
               m_Instance = new T;
            }
            m_InstanceCnt++;
            
            return m_Instance;
         }
         
         /**
          * frees an instance to the single class
          */
         static void releaseInstance( void )
         {
            if( m_InstanceCnt )
            {
               if( --m_InstanceCnt == 0 )
               {
                  delete m_Instance;
                  m_Instance = 0;
               }
            }
            
            return;
         }
         
      protected:
         
         /**
          *constructor
          */
         c_Singleton( void )
         {
         }
         
         /**
          *destructor
          */
         ~c_Singleton( void )
         {
         }
         
         
      private:
         
         /**
          * unimplemeneted
          */
         c_Singleton(const c_Singleton& );
         /**
          * unimplemeneted
          */
         c_Singleton& operator=(const c_Singleton& );
         
      private:
         static T*            m_Instance;  
         static unsigned long m_InstanceCnt;  
         
   };
 
   template <typename T> T* c_Singleton<T>::m_Instance               = 0;       
   template <typename T> unsigned long c_Singleton<T>::m_InstanceCnt = 0;      

}
   
#endif //WII_GI_SINGLETON_H
